package com.client.data;

import java.util.ArrayList;
import java.util.List;

import com.client.forms.CompagnieEditor;
import com.client.service.data.CompagnieDataService;
import com.client.service.data.CompagnieDataServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.shared.model.Compagnie;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
/**
 * Panel de gestion {@link Compagnie}
 * @author queyroi
 *
 */
public class PanelBaseCompagnie {
	/**
	 * Liste des {@link Compagnie} disponible (chargé par {@link CompagnieDataService})
	 */
	private List<Compagnie> liste =  new ArrayList<Compagnie>();
	/**
	 * Tableaux des {@link Compagnie} 
	 */
	private CompagnieListGrid listGrid = new CompagnieListGrid();
	/**
	 * Panel d'édition d'une {@link Compagnie} 
	 */
	private CompagnieEditor dataEditor =  new CompagnieEditor();
	/**
	 * Service RPC {@link Compagnie} 
	 */
	private final CompagnieDataServiceAsync dataService = GWT.create(CompagnieDataService.class);
	
	private int indexSelected = 0;
	
	private HorizontalPanel buttonPanel = new HorizontalPanel();
	/**
	 * Sauvegarde de la {@link Compagnie} éditée par le {@link CompagnieEditor} dans le DataStore
	 */
	private Button addButton = new Button();
	/**
	 * Suppresion de la {@link Compagnie} sélectionné dans le DataStore
	 */
	private Button removeButton = new Button();
	/**
	 * Mise à jour de la {@link Compagnie} éditée par le {@link CompagnieEditor} dans le DataStore
	 */
	private Button updateButton = new Button();
	/**
	 * Vider le {@link CompagnieEditor}
	 */
	private Button clearEditorButton = new Button();
	
	public PanelBaseCompagnie() {
		
		listGrid.addCellClickHandler(new CellClickHandler() {
			
			public void onCellClick(CellClickEvent event) {
				removeButton.setEnabled(true);
				updateButton.setEnabled(true);				
				indexSelected = event.getRowNum();
				dataEditor.setEdited(liste.get(indexSelected));
				dataEditor.fill();
				}
			});

		dataService.getList(new AsyncCallback<List>() {
			
			public void onSuccess(List result) {
				liste = result;
				fillGrid();
			}
			
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
				
			}
		});
		
		addButton.addClickHandler(new ClickHandler() {			
			public void onClick(ClickEvent event) {
				try{
					final Compagnie compagnie = dataEditor.getEdited(); 
					compagnie.setKey(null);
					dataService.save(compagnie, new AsyncCallback<String>() {						
						public void onSuccess(String result) {
							liste.add(compagnie);
							listGrid.addData(compagnie);
						}						
						public void onFailure(Throwable caught) {
							Window.alert(caught.getMessage());
							
						}
					});
				}catch(IllegalArgumentException e){
					Window.alert(e.getMessage());
				}
			}
		});
		
		updateButton.addClickHandler(new ClickHandler() {			
			public void onClick(ClickEvent event) {
				try{				
					final Compagnie compagnie = dataEditor.getEdited();	
					
					compagnie.setKey(liste.get(indexSelected).getKey());
					dataService.update( compagnie, new AsyncCallback<String>() {						
						public void onSuccess(String result) {
							liste.remove(indexSelected);
							liste.add(indexSelected, compagnie);
							listGrid.removeAll();
							fillGrid();
							
						}						
						public void onFailure(Throwable caught) {
							Window.alert(caught.getMessage());
							
						}
					});
				}catch(IllegalArgumentException e){
					Window.alert(e.getMessage());
				}
			}
		});
		
		removeButton.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				dataService.delete(liste.get(indexSelected), new AsyncCallback<String>() {
					
					public void onSuccess(String result) {
						liste.remove(indexSelected);
						listGrid.removeData(listGrid.getSelectedRecord());	
						dataEditor.setEmpty();
					}
					
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());						
					}
				});				
			}
		});
		clearEditorButton.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				dataEditor.setEmpty();
				
			}
		});
		
	}
	/**
	 * Creation du Panel
	 * @return le conteneur de base du panel
	 */
	public VerticalPanel createPanel(){
		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.setSpacing(5);
		mainPanel.add(listGrid);
		
		buttonPanel.setSpacing(5);
		
		addButton.setText("Ajouter");
		buttonPanel.add(addButton);
		
		
		updateButton.setText("Mettre à jour");
		buttonPanel.add(updateButton);
		updateButton.setEnabled(false);
		
		removeButton.setText("Enlever");
		buttonPanel.add(removeButton);
		removeButton.setEnabled(false);
		
		clearEditorButton.setText("Vider Editeur");
		buttonPanel.add(clearEditorButton);
		
		mainPanel.add(buttonPanel);		
		
		mainPanel.add(dataEditor);
		
		return(mainPanel);
	}
	/**
	 * Méthode de remplissage de la {@link CompagnieListGrid}
	 */
	private void fillGrid(){
		for(Compagnie p : liste){
			listGrid.addData(p);			
		}
	}
	

}
